const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function deepClone(origin, hashMap = new WeakMap()) {
  // undefined null 原始值
  if (origin == undefined || typeof origin !== 'object') {
    return origin;
  }
  // Date
  if (origin instanceof Date) {
    return new Date(origin)
  }
  // RegExp
  if (origin instanceof RegExp) {
    return new RegExp(origin)
  }
  // 检测对象是否被拷贝过 
  const hasKey = hashMap.get(origin);
  if (hasKey) {
    return hasKey; // 拷贝过 之间返回上一次拷贝结果 无需再次拷贝
  }
  // origin 此时为 {} || [] 
  const target = new origin.constructor();
  hashMap.set(origin, target); // 保存记录 origin 已被拷贝
  for (let k in origin) {
    if (origin.hasOwnProperty(k)) {
      target[k] = deepClone(origin[k], hashMap);
    }
  }
  return target;
}

function isObject(data) {
  return typeof data === 'object' && data !== null;
}

function isArray(value) {
  return Array.isArray(value);
}

function isEmptyValue(value) {
  return typeof value === 'undefined' || value === null;
}

 function combineData(target, data) {
  if (isArray(data)) {
    target = target || [];
    if (target.length > data.length && data.length) {
      target.forEach((item, index) => {
        if (isObject(data[0])) {
          item = combineData(item, deepClone(data[0]));
        } else {
          if (isEmptyValue(item)) {
            target[index] = data[0];
          }
        }
      });
    } else {
      // 合并数组
      data.forEach((item, index) => {
        if (isObject(item)) {
          target[index] = combineData(target[index], deepClone(item));
        } else {
          // 基础值
          if (isEmptyValue(target[index])) {
            target[index] = item;
          }
        }
      });
    }
  } else if (isObject(data)) {
    // 合并对象
    // 遍历data对象
    target = target || {};
    Object.keys(data).forEach((key) => {
      // 原数据没有 key 属性
      if (!target.hasOwnProperty(key)) {
        if (isObject(data[key])) {
          target[key] = deepClone(data[key]);
        } else {
          if (isEmptyValue(target[key])) {
            target[key] = deepClone(data[key]);
          }
        }
      } else {
        if (isObject(data[key])) {
          target[key] = combineData(target[key], data[key]);
        }
      }
    });
  } else {
    // 基础值 不做任何处理
  }
  return target;
}

function debounce(fn, time, triggleNow) {
  let t = null;
  let res = null;
  let debounced = function() {
    let _self = this,
      args = arguments;
    if (t) {
      clearTimeout(t);
    }
    // 立即触发
    if (triggleNow) {
      let exec = !t;
      t = setTimeout(function() {
        // time 秒后 清除 t 
        t = null;
      }, time);
      if (exec) {
        res = fn.apply(_self, args);
      }
    } else {
      t = setTimeout(function() {
        // 延时执行
        res = fn.apply(_self, args);
      }, time);
    }
    return res;
  }
  debounced.remove = function() {
    clearTimeout(t);
    t = null;
  }
  return debounced;
}

const prefixString = 'plugin-private://wx9d384f1607927496';

module.exports = {
  formatTime: formatTime,
  deepClone,
  combineData,
  isObject,
  isArray,
  debounce,
  prefixString
}
